﻿define(["dojo", "dijit/layout/ContentPane"],
	function(dojo, ContentPane)
	{
		return dojo.declare("Aras.Client.UI.Components.MarkupViewer", ContentPane, {
			constructor: function(args)
			{
				var params = {};
				params.hide = false;
				params.startPage = 1;
				params.messageId = null;
				params.parentMessageId = null;
				params.readOnly = false;

				top.VC_ViewerParamsObject = params;

				var currentItemId = top.itemID;

				switch (top.itemTypeName)
				{
					case "Document":
						if (localStorage.hasOwnProperty("VC_ViewerParamsObject"))
						{
							params = JSON.parse(localStorage["VC_ViewerParamsObject"]);
							top.VC_ViewerParamsObject = params;
							localStorage.removeItem("VC_ViewerParamsObject");
						}
						else
						{
							var res = this.getFirstFoundPdfDocumentFromDocument(currentItemId);
							if (!res)
								res = this.getFirstFoundImageFromDocument(currentItemId);

							if (!res)
								return;

							params.viewerMode = +3;//SSVC.Utils.ViewerMode.View
							params.fileId = res.fileId;
							params.fileUrl = res.fileUrl;
							params.fileType = res.fileType;
						}

						args.style = "width: 500px;";
						args.splitter = true;
						if (params.fileType == "pdf")
							args.content = "<iframe id='ViewerFrame' src='../javascript/pdfjs/web/viewer.html?file=" + encodeURIComponent(params.fileUrl) + "#page=" + params.startPage + "' width='100%' height='100%' />";
						else
							args.content = "<iframe id='ViewerFrame' src='../Modules/aras.innovator.SSVC/scripts/ImageViewer.html' width='100%' height='100%' />";
						break;
					case "CAD":
						if (localStorage.hasOwnProperty("VC_ViewerParamsObject"))
						{
							params = JSON.parse(localStorage["VC_ViewerParamsObject"]);
							top.VC_ViewerParamsObject = params;
							localStorage.removeItem("VC_ViewerParamsObject");
						}
						else
						{
							var res = this.getFirstFoundHwfDocumentFromCAD(currentItemId);
							if (!res)
								return;

							params.viewerMode = +3;//SSVC.Utils.ViewerMode.View
							params.documentId = currentItemId;
							params.documentType = "CAD";
							params.fileId = res.fileId;
							params.fileUrl = encodeURIComponent(res.fileUrl);
							params.fileType = res.fileType;
						}

						args.style = "width: 500px";
						args.splitter = true;
						args.content = "<iframe id='ViewerFrame' src='../Modules/aras.innovator.SSVC/scripts/HoopsViewer.html' width='100%' height='100%' />";
						break;
					case "Part":
						//Get current part with the first found image file
						var aml =
							"<AML>" +
								"<Item type='Part' action='get' id='" + currentItemId + "' select='thumbnail' />" +
							"</AML>";

						var partItem = new Item("Part");
						partItem.loadAML(aml);
						partItem = partItem.apply();

						if (partItem.isError())
						{
							return;
						}

						var thumbnailVaultUrl = partItem.getProperty("thumbnail", "");
						var thumbnailId = thumbnailVaultUrl.replace(/vault:\/\/\/\?fileid\=/i, "");

						if (thumbnailId == "")
						{
							//No file was found
							top.aras.AlertError("File not found!");
							return;
						}

						var imageItm = new Item("File", "get");
						imageItm.setID(thumbnailId);
						imageItm = imageItm.apply();

						if (imageItm.isError())
						{
							return;
						}

						var fileName = imageItm.getProperty("filename", "");
						var fileExtension = fileName.split('.').pop();

						var imageExtensionsList = ["png", "jpg", "jpeg", "bmp", "gif", "tif", "tiff", "svg"];

						if (imageExtensionsList.indexOf(fileExtension) == -1)
						{
							//It's not an image
							top.aras.AlertError("File is not an image!");
							return;
						}

						params.fileId = thumbnailId;
						params.fileUrl = top.aras.IomInnovator.getFileUrl(thumbnailId, Enums.UrlType.SecurityToken);

						args.style = "width: 500px";
						args.splitter = true;
						args.content = "<iframe id='ViewerFrame' src='../Modules/aras.innovator.SSVC/scripts/ImageViewer.html' width='100%' height='100%' />";
						break;
					default:
						args.content = "";
				}
				this.args = args;
			},

			postCreate: function()
			{
				var self = this;
				dojo.connect(this, "startup", function()
				{
					//do someting
				});
			},

			getFirstFoundPdfDocumentFromDocument: function(docId)
			{
				//Get current document with the first found pdf file
				var aml =
						"<AML>" +
							"<Item type='Document' action='get' id='" + docId + "'>" +
								"<Relationships>" +
									"<Item type='Document File' action='get' orderBy='sort_order' maxRecords='1'>" +
										"<related_id>" +
											"<Item type='File' action='get'>" +
												"<file_type>" +
													"<Item type='FileType' action='get'>" +
														"<extension>pdf</extension>" +
													"</Item>" +
												"</file_type>" +
											"</Item>" +
										"</related_id>" +
									"</Item>" +
								"</Relationships>" +
							"</Item>" +
						"</AML>";

				var documentItem = top.aras.newIOMItem("Document");
				documentItem.loadAML(aml);
				documentItem = documentItem.apply();

				if (documentItem.isError())
					return;

				//Get first found pdf file
				var fileItem = documentItem.getItemsByXPath("Relationships/Item/related_id/Item");

				if (fileItem.getItemCount() == 0)
				{
					//No pdf file was found
					//top.aras.AlertError(SSVC.Utils.GetResource("feed_pdf_not_found"));
					return;
				}

				var fileId = fileItem.getID();
				var fileUrl = top.aras.IomInnovator.getFileUrl(fileId, top.aras.Enums.UrlType.SecurityToken);

				return {
					fileId: fileId,
					fileUrl: fileUrl,
					fileType: "pdf"
				};
			},

			getFirstFoundImageFromDocument: function(docId)
			{
				//Get current document with the first found pdf file
				var aml =
						"<AML>" +
							"<Item type='Document' action='get' id='" + docId + "'>" +
								"<Relationships>" +
									"<Item type='Document File' action='get' orderBy='sort_order' maxRecords='1'>" +
										"<related_id>" +
											"<Item type='File' action='get'>" +
												"<file_type>" +
													"<Item type='FileType' action='get'>" +
														"<or>" +
															"<extension>png</extension>" +
															"<extension condition='like'>*jpg*</extension>" +
															"<extension>gif</extension>" +
														"</or>" +
													"</Item>" +
												"</file_type>" +
											"</Item>" +
										"</related_id>" +
									"</Item>" +
								"</Relationships>" +
							"</Item>" +
						"</AML>";

				var documentItem = top.aras.newIOMItem("Document");
				documentItem.loadAML(aml);
				documentItem = documentItem.apply();

				if (documentItem.isError())
					return;

				var fileItem = documentItem.getItemsByXPath("Relationships/Item/related_id/Item");

				if (fileItem.getItemCount() == 0)
					return;

				var fileId = fileItem.getID();
				var fileUrl = top.aras.IomInnovator.getFileUrl(fileId, top.aras.Enums.UrlType.SecurityToken);

				return {
					fileId: fileId,
					fileUrl: fileUrl,
					fileType: "image"
				};
			},

			getFirstFoundHwfDocumentFromCAD: function(cadId)
			{
				var cadItem = top.aras.newIOMItem("CAD", "get");
				cadItem.setProperty("id", cadId);
				cadItem = cadItem.apply();

				if (cadItem.getProperty("view_file", "") == "")
				{
					top.aras.AlertError(SSVC.Utils.GetResource("feed_hwf_not_found"));
					return;
				}

				var fileId = cadItem.getProperty("view_file");
				var fileUrl = top.aras.IomInnovator.getFileUrl(fileId, top.aras.Enums.UrlType.SecurityToken);

				return {
					fileId: fileId,
					fileUrl: fileUrl,
					fileType: "hwf"
				};
			}
		});
	});